
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;

/**
* @author :${author}
* @description: ${desc}服务接口实现类
* @create date: ${createDate}
*/

@Slf4j
@Service
public class ${entityName}ServiceImpl implements ${entityName}Service {

    @Autowired
    ${entityName}Mapper ${entityNameLower}Mapper;

    /**
    * 保存${desc}
    * @param ${entityNameLower}
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(${entityName} ${entityNameLower}) {
        return ${entityNameLower}Mapper.insert(${entityNameLower});
    }

    /**
    * 根据ID更新${desc}
    * @param ${entityNameLower}
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(${entityName} ${entityNameLower}) {
        return ${entityNameLower}Mapper.updateById(${entityNameLower});
    }

    /**
    * 删除${desc}
    * @param ${entityNameLower}
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(${entityName} ${entityNameLower}) {
        <#if primaryKey!='noPrimaryKey'>
        return ${entityNameLower}Mapper.deleteById(${entityNameLower}.${primaryKeyGet});
        <#else>
        // TODO 没有主键
        //return ${entityNameLower}Mapper.deleteById(${entityNameLower});
        return -1;
        </#if>
    }

    /**
    * 根据查询${desc}
    * @param ${entityNameLower}
    * @return
    */
    @Override
    public ${entityName}Vo detail(${entityName} ${entityNameLower}) {
        <#if primaryKey!='noPrimaryKey'>
        ${entityNameLower}= ${entityNameLower}Mapper.selectById(${entityNameLower}.${primaryKeyGet});
        return ${beanUtils}.copyTo(${entityNameLower}, ${entityName}Vo.class);
        <#else>
        // TODO 没有主键
        //${entityName} ${entityNameLower}= ${entityNameLower}Mapper.selectById(${primaryKey});
        //return ${beanUtils}.copyTo(${entityNameLower}, ${entityName}Vo.class);
        return null;
        </#if>

    }

    /**
    * 查询${desc}
    * @param ${entityNameLower}
    * @return
    */
    @Override
    public List<${entityName}Vo> findList(${entityName} ${entityNameLower}) {
        QueryWrapper<${entityName}> queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(${entityNameLower});
        List<${entityName}> list= ${entityNameLower}Mapper.selectList(queryWrapper);
        return ${beanUtils}.copyList(list,${entityName}Vo.class);
    }

    /**
    * 查询${desc}分页信息
    * @param ${entityNameLower}PageDto
    * @return PageVO
    */


    @Override
    public PageVo findPageList(${entityName}PageDto ${entityNameLower}PageDto) {
        Page<${entityName}> page=new Page<>(${entityNameLower}PageDto.getPageNum(),${entityNameLower}PageDto.getPageSize());
        ${entityName} ${entityNameLower} = BeanExtUtil.copyTo(${entityNameLower}PageDto, ${entityName}.class);

        QueryWrapper<${entityName}> queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(${entityNameLower});
        // page.addOrder(OrderItem.desc("id"));
        ${entityNameLower}Mapper.selectPage(page, queryWrapper);

        //自定义sql 分页方式
        //page=${entityNameLower}Mapper.selectPageList(page, ${entityNameLower});
        //List<${entityName}> records = page.getRecords();
        return new TableDataInfo(${beanUtils}.copyList(records,${entityName}Vo.class), Math.toIntExact(page.getTotal()));
    }


   }